package cn.openea.perpartner.payment.web.coupon.service.impl;

import cn.openea.perpartner.payment.web.coupon.entity.CouponRecord;
import cn.openea.perpartner.payment.web.coupon.entity.Coupon;
import cn.openea.perpartner.payment.web.coupon.mapper.CouponRecordMapper;
import cn.openea.perpartner.payment.web.coupon.service.ICouponRecordService;
import cn.openea.perpartner.payment.web.coupon.service.ICouponService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券使用记录表(CouponUsage)表服务实现类
 *
 * @author Weird077
 * @since 2023-04-14 14:29:44
 */
@Service("couponRecordService")
public class CouponRecordServiceImpl extends ServiceImpl<CouponRecordMapper, CouponRecord> implements ICouponRecordService {
    @Autowired
    private ICouponService couponService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean useCoupon(CouponRecord coupon) {
        couponService.update(Wrappers.<Coupon>lambdaUpdate().set(Coupon::getStatus, "1")
                .eq(Coupon::getId, coupon.getId())
                .eq(Coupon::getStatus, "0"));
        coupon.setId(null);
        return save(coupon);
    }
}

